package com.rangter.MissedCalls.Utility;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import android.util.Log;

public class TimeUtility {
	
	private TimeUtility(){}
	
	public static TimeUtility getTimeInstance()
	{
		return new TimeUtility();
	}
	
	public int getIntDate(String date)
	{
		final   long   DAY   =   3600*1000*24;   
        DateFormat   f   =   new   SimpleDateFormat("yyyy-MM-dd");   
        try {
			Date d1   = f.parse(date);
			Date d2   = f.parse("1949-10-01"); 
			int res = (int) ((d1.getTime()-d2.getTime())/DAY);
			Log.i("----",String.valueOf(res));
			return res;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
		
        
		return 0;
	}
	
	public String getStrDate(int date)
	{
		final long DAY1 = 3600*1000*24;
		SimpleDateFormat f   = new SimpleDateFormat("yyyy-MM-dd");
		 try {
//				Date   d1   =   (Date) f.parse(date);
				Date   d2   =   f.parse("1949-10-01"); 
				long ddd = (date+d2.getTime()/DAY1)*DAY1;
				Date dd = new Date(ddd);
				Log.i("----",String.valueOf(dd));
				
				String res = getDateStr(dd);
				Log.i("----",String.valueOf(res));
				return res;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
			}
		return null;
	}
	
	public String getDateStr(Date date)
	{
		int year = date.getYear()+1900;
		int month = date.getMonth()+1;
		String monthStr = month>9?String.valueOf(month):"0"+String.valueOf(month);
		int day = date.getDate();
		String dayStr = day>9?String.valueOf(day):"0"+String.valueOf(day);
		String res = String.valueOf(year)+"-"+monthStr+"-"+dayStr;
		return res;
	}
	
	public int getToday()
	{
		Date a = new Date();
		return getIntDate(getDateStr(a));
		
	}
	
	public int getYesterDay()
	{
		Date a = new Date();
		int today = getIntDate(getDateStr(a));
		return (today-1);
	}
	
	public int getDayBeforeMonth()
	{
		Date a = new Date();
		Log.i("now111", a.toString());
		int today = getIntDate(getDateStr(a));
		Log.i("now", getDateStr(a));
		return (today-30);
	}
}
